/*
 *接口编写处...
 *如果接口需要做Action的权限验证，请在Action上使用属性
 *如: [ApiActionPermission("Inventory",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.demo.IServices;
using VOL.Core.Filters;

namespace VOL.demo.Controllers
{
    public partial class InventoryController
    {
        private readonly IInventoryService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public InventoryController(
            IInventoryService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 添加库存信息
        /// </summary>
        /// <param name="inventory">库存信息</param>
        /// <returns></returns>
        [HttpPost("AddInventory")]
        public IActionResult AddInventory([FromBody] InventoryModel inventory)
        {
            try
            {
                if (inventory == null)
                {
                    return Json(new { success = false, message = "库存信息不能为空" });
                }

                var result = _service.AddInventory(inventory);
                if (result.Status)
                {
                    return Json(new { success = true, message = result.Message, data = result.Data });
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 批量添加库存信息
        /// </summary>
        /// <param name="inventories">库存列表</param>
        /// <returns></returns>
        [HttpPost("BatchAddInventory")]
        public IActionResult BatchAddInventory([FromBody] List<InventoryModel> inventories)
        {
            try
            {
                if (inventories == null || inventories.Count == 0)
                {
                    return Json(new { success = false, message = "库存信息不能为空" });
                }

                var result = _service.BatchAddInventory(inventories);
                if (result.Status)
                {
                    return Json(new { success = true, message = result.Message, data = result.Data });
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 根据货品ID获取库存信息
        /// </summary>
        /// <param name="goodsId">货品ID</param>
        /// <returns></returns>
        [HttpGet("GetInventoryByGoodsId")]
        public IActionResult GetInventoryByGoodsId(long goodsId)
        {
            try
            {
                if (goodsId <= 0)
                {
                    return Json(new { success = false, message = "货品ID无效" });
                }

                var result = _service.GetInventoryByGoodsId(goodsId);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 根据仓库ID获取库存信息
        /// </summary>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns></returns>
        [HttpGet("GetInventoryByWarehouseId")]
        public IActionResult GetInventoryByWarehouseId(long warehouseId)
        {
            try
            {
                if (warehouseId <= 0)
                {
                    return Json(new { success = false, message = "仓库ID无效" });
                }

                var result = _service.GetInventoryByWarehouse(warehouseId);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 根据货品ID和仓库ID获取库存信息
        /// </summary>
        /// <param name="goodsId">货品ID</param>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns></returns>
        [HttpGet("GetInventoryByGoodsAndWarehouse")]
        public IActionResult GetInventoryByGoodsAndWarehouse(long goodsId, long warehouseId)
        {
            try
            {
                if (goodsId <= 0 || warehouseId <= 0)
                {
                    return Json(new { success = false, message = "货品ID和仓库ID不能为空" });
                }

                var result = _service.GetInventoryByGoodsAndWarehouse(goodsId, warehouseId);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 库存调整 - 增加库存
        /// </summary>
        /// <param name="request">库存调整请求</param>
        /// <returns></returns>
        [HttpPost("IncreaseInventory")]
        public IActionResult IncreaseInventory([FromBody] InventoryAdjustRequest request)
        {
            try
            {
                if (request == null || request.InventoryId <= 0 || request.Quantity <= 0)
                {
                    return Json(new { success = false, message = "调整参数无效" });
                }

                var result = _service.AdjustInventory(request.InventoryId, request.Quantity, request.Reason);
                if (result.Status)
                {
                    return Json(new { success = true, message = result.Message });
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 库存调整 - 减少库存
        /// </summary>
        /// <param name="request">库存调整请求</param>
        /// <returns></returns>
        [HttpPost("DecreaseInventory")]
        public IActionResult DecreaseInventory([FromBody] InventoryAdjustRequest request)
        {
            try
            {
                if (request == null || request.InventoryId <= 0 || request.Quantity <= 0)
                {
                    return Json(new { success = false, message = "调整参数无效" });
                }

                var result = _service.AdjustInventory(request.InventoryId, -request.Quantity, request.Reason);
                if (result.Status)
                {
                    return Json(new { success = true, message = result.Message });
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 获取低库存预警信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetLowStockAlert")]
        public IActionResult GetLowStockAlert()
        {
            try
            {
                var result = _service.GetLowStockAlert();
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 获取即将过期的库存信息
        /// </summary>
        /// <param name="days">提前天数（默认30天）</param>
        /// <returns></returns>
        [HttpGet("GetExpiringSoonInventory")]
        public IActionResult GetExpiringSoonInventory(int days = 30)
        {
            try
            {
                var result = _service.GetExpiringSoonInventory(days);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 更新库存信息
        /// </summary>
        /// <param name="inventory">库存信息</param>
        /// <returns></returns>
        [HttpPost("UpdateInventory")]
        public IActionResult UpdateInventory([FromBody] InventoryModel inventory)
        {
            try
            {
                if (inventory == null || inventory.Id <= 0)
                {
                    return Json(new { success = false, message = "库存ID无效" });
                }

                var result = _service.UpdateInventory(inventory);
                if (result.Status)
                {
                    return Json(new { success = true, message = result.Message });
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 删除库存信息
        /// </summary>
        /// <param name="id">库存ID</param>
        /// <returns></returns>
        [HttpDelete("DeleteInventory/{id}")]
        public IActionResult DeleteInventory(long id)
        {
            try
            {
                if (id <= 0)
                {
                    return Json(new { success = false, message = "库存ID无效" });
                }

                var result = _service.DeleteInventory(id);
                if (result.Status)
                {
                    return Json(new { success = true, message = result.Message });
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 批量设置库存上下限
        /// </summary>
        /// <param name="request">批量设置请求</param>
        /// <returns></returns>
        [HttpPost("BatchSetInventoryLimits")]
        public IActionResult BatchSetInventoryLimits([FromBody] BatchSetInventoryLimitsRequest request)
        {
            try
            {
                if (request == null || request.InventoryIds == null || request.InventoryIds.Count == 0)
                {
                    return Json(new { success = false, message = "库存ID列表不能为空" });
                }

                var result = _service.BatchSetInventoryLimits(request.InventoryIds, request.UpperLimit, request.LowerLimit);
                if (result.Status)
                {
                    return Json(new { success = true, message = result.Message });
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 获取库存统计信息
        /// </summary>
        /// <param name="warehouseId">仓库ID（可选）</param>
        /// <returns></returns>
        [HttpGet("GetInventoryStatistics")]
        public IActionResult GetInventoryStatistics(long? warehouseId = null)
        {
            try
            {
                var result = _service.GetInventoryStatistics(warehouseId);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 根据批次号获取库存信息
        /// </summary>
        /// <param name="batchNo">批次号</param>
        /// <returns></returns>
        [HttpGet("GetInventoryByBatchNo")]
        public IActionResult GetInventoryByBatchNo(string batchNo)
        {
            try
            {
                if (string.IsNullOrEmpty(batchNo))
                {
                    return Json(new { success = false, message = "批次号不能为空" });
                }

                var result = _service.GetInventoryByBatchNo(batchNo);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }
    }

    /// <summary>
    /// 库存调整请求模型
    /// </summary>
    public class InventoryAdjustRequest
    {
        public long InventoryId { get; set; }
        public int Quantity { get; set; }
        public string Reason { get; set; }
    }

}
